﻿using HardcoreGame.Content.Items.Consumables.Others;
using Microsoft.Xna.Framework.Graphics;
using ReLogic.Content;
using System.Collections.Generic;
using Terraria;
using Terraria.GameContent;
using Terraria.ModLoader;

namespace HardcoreGame.Common.UI
{
    public class VanillaOverlay : ModResourceOverlay
    {
        private Dictionary<string, Asset<Texture2D>> vanillaAssetCache = [];
        private Asset<Texture2D> starTexture, barsFillingTexture;
        public override void PostDrawResource(ResourceOverlayDrawContext context)
        {
            Asset<Texture2D> asset = context.texture;
            string fancyFolder = "Images/UI/PlayerResourceSets/FancyClassic/";
            string barsFolder = "Images/UI/PlayerResourceSets/HorizontalBars/";

            if (Main.LocalPlayer.GetModPlayer<ManaFruitPlayer>().goldenStars > 0)
            {
                if (asset == TextureAssets.Mana)
                {
                    context.texture = starTexture ??= ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Players/Mana");
                    context.Draw();
                }
                else if (CompareAssets(asset, fancyFolder + "Star_Fill"))
                {
                    context.texture = starTexture ??= ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Players/Star_Fill");
                    context.Draw();
                }
                else if (CompareAssets(asset, barsFolder + "MP_Fill"))
                {
                    context.texture = barsFillingTexture ??= ModContent.Request<Texture2D>("HardcoreGame/Assets/Textures/Players/MP_Fill");
                    context.Draw();
                }
            }
        }
        private bool CompareAssets(Asset<Texture2D> existingAsset, string compareAssetPath)
        {
            if (!vanillaAssetCache.TryGetValue(compareAssetPath, out var asset))
            {
                asset = vanillaAssetCache[compareAssetPath] = Main.Assets.Request<Texture2D>(compareAssetPath);
            }
            return existingAsset == asset;
        }
    }
}
